function hfile = prt_ShiftOnsets(hfile, shiftval, conds)
% PRT::ShiftOnsets  - shift onsets in conditions of a PRT
%
% FORMAT:       [prt =] prt.ShiftOnsets(shiftval [, conds])
%
% Input fields:
%
%       shiftval    1x1 double value that will be added to OnOffsets
%       conds       optional condition selection (pattern or list, def: all)
%
% Output fields:
%
%       prt         altered PRT
%
% Examples:
%
%   prt.ShiftOnsets(4000);
%   prt.ShiftOnsets(2000, '.*response');
%

% Version:  v0.7f
% Build:    9030420
% Date:     Mar-04 2009, 8:43 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 2 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'prt') || ...
   ~isa(shiftval, 'double') || ...
    numel(shiftval) ~= 1 || ...
    isinf(shiftval) || ...
    isnan(shiftval)
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end
bc = bvqxfile_getcont(hfile.L);

% get number of conditions
ncon = numel(bc.Cond);

% calc new color ?
if nargin < 3 || ...
   (~ischar(conds) && ...
    ~isa(conds, 'double'))
    conds = 1:ncon;
elseif ischar(conds)
    tconds = false(1, ncon);
    conds = conds(:)';
    for cc = 1:ncon
        if ~isempty(regexpi(bc.Cond(cc).ConditionName{1}, conds))
            tconds(cc) = true;
        end
    end
    conds = find(tconds);
else
    conds = conds(:)';
    conds(isinf(conds) | isnan(conds) | conds < 1 | conds > ncon) = [];
    conds = unique(round(conds));
end

for cc = conds
    bc.Cond(cc).OnOffsets = bc.Cond(cc).OnOffsets + shiftval;
end

% set new content
bvqxfile_setcont(hfile.L, bc);
